package dao

import (
	"fmt"
	"time"
)

type Comment struct {
	UserID      string
	GoodID      string
	CommentInfo string
	Score       string
}

type CommentRes struct {
	CommentID  int       `json:"comment_id"`
	UserID     int       `json:"user_id"`
	GoodId     int       `json:"good_id"`
	Context    string    `json:"context"`
	CreateTime time.Time `json:"create_time"`
	Score      int       `json:"score"`
}

func AddComment(info Comment) (err error) {
	//sqlStr := "insert into comment_db values (null, ?,?, '?' , null, ?)"
	sqlStr := fmt.Sprintf("insert into comment_db (user_id, good_idfk, context, score) values ( %s, %s, '%s', %s)",
		info.UserID, info.GoodID, info.CommentInfo, info.Score)
	//sqlStr := "insert into comment_db values (null, ?,?, ? , null, ?)"
	//fmt.Println(sqlStr, info.UserID, info.GoodID, info.CommentInfo, info.Score)
	_, err = DB.Exec(sqlStr)
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println(info.UserID + "评论" + info.GoodID + ":" + info.CommentInfo)
	return
}

func QueryComment(gid string, code int) (all []CommentRes) {
	sqlStr := "select * from comment_db where good_idfk = ?"
	if code == 5 {
		sqlStr = sqlStr + " && (score=5) "
	} else if code == 3 {
		sqlStr = sqlStr + " && (score=4||score=3)"
	} else if code == 1 {
		sqlStr = sqlStr + " && (score=1||score=2)"
	}
	rows, err := DB.Query(sqlStr, gid)
	defer rows.Close()
	if err != nil {
		fmt.Println("QueryComment err: ", err)
		return
	}
	for rows.Next() {
		var temp CommentRes
		err = rows.Scan(
			&temp.CommentID,
			&temp.UserID,
			&temp.GoodId,
			&temp.Context,
			&temp.CreateTime,
			&temp.Score,
		)
		if err != nil {
			fmt.Println("CommentRes err ", err)
			return nil
		}
		all = append(all, temp)
	}
	return
}
